/*
 * @Description: 图片构建组件
 * @Author: paddies
 * @Date: 2023-05-26 22:36:43
 * @LastEditors: paddies
 * @LastEditTime: 2025-07-23 01:55:06
 * @FilePath: /shared_components/lib/build-image/buildImage.dart
 */
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:shared_components/build-image/PhotoViewPage.dart';

Widget buildImage(
    {String url = "",
    bool provider = false,
    String type = "ClipOval",
    BoxFit fit = BoxFit.cover,
    double width = double.infinity,
    double radius = 0.0,
    BorderRadiusGeometry? borderRadius,
    double height = double.infinity}) {
  if (url.startsWith("assets/")) {
    return SizedBox(
        height: height,
        width: width,
        child: Image.asset(
          url,
          fit: fit,
        ));
  }

  // if (provider) {
  //   return PhotoView(
  //     imageProvider: CachedNetworkImageProvider(url),
  //     minScale: PhotoViewComputedScale.contained * 0.8,
  //     maxScale: PhotoViewComputedScale.covered * 1.8,
  //     initialScale: PhotoViewComputedScale.contained,
  //   );
  // }

  switch (type) {
    case "ClipOval":
      return SizedBox(
        height: height,
        width: width,
        child: ClipOval(
          child: CachedNetworkImage(
            imageUrl: url,
            imageBuilder: (context, imageProvider) => Container(
              decoration: BoxDecoration(
                borderRadius: BorderRadius.all(Radius.circular(radius)), //圆角

                image: DecorationImage(
                  image: imageProvider,
                  fit: fit,
                  // colorFilter:
                  //     ColorFilter.mode(Colors.red, BlendMode.colorBurn)
                ),
              ),
            ),
            placeholder: (context, url) => Stack(
              alignment: Alignment.center,
              children: [
                // Container(
                //   width: double.infinity,
                //   height: double.infinity,
                //   // color: Colors.red,
                //   child: Image.asset(
                //     'assets/images/loading.gif',
                //     fit: BoxFit.fill,
                //   ),
                //   // child: const CircularProgressIndicator(),
                // )
              ],
            ),
            errorWidget: (context, url, error) =>
                const Icon(Icons.image_not_supported_sharp),
          ),

          // Image.network(
          //   url,
          //   height: height,
          //   width: width,
          //   fit: fit,
          // ),
        ),
      );

    case "ClipRect":
      return InkWell(
          onTap: provider
              ? () {
                  if (provider) {
                    Navigator.push(
                        Get.overlayContext!,
                        MaterialPageRoute(
                          builder: (context) => PhotoViewPage(imageUrl: url),
                        ));
                  }
                  return;
                }
              : null,
          child: Container(
            height: height,
            width: width,
            child: ClipRect(
              child: CachedNetworkImage(
                imageUrl: url,
                imageBuilder: (context, imageProvider) => Container(
                  decoration: BoxDecoration(
                    borderRadius: borderRadius == null
                        ? BorderRadius.all(Radius.circular(radius))
                        : borderRadius, //圆角

                    image: DecorationImage(
                      image: imageProvider,
                      fit: fit,
                      // colorFilter:
                      //     ColorFilter.mode(Colors.red, BlendMode.colorBurn)
                    ),
                  ),
                ),
                placeholder: (context, url) => Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    // Container(
                    //   width: 20,
                    //   height: 20,
                    //   child: CircularProgressIndicator(),
                    // ),
                  ],
                ),

                // Container(
                //   width: double.infinity,
                //   height: double.infinity,

                // child: Stack(
                //   alignment: Alignment.center,
                //   children: [
                //     const ProgressLoading(
                //         isShow: true, showType: AnimationType.refresh)
                //   ],
                // ),
                // ),
                errorWidget: (context, url, error) =>
                    const Icon(Icons.image_not_supported_sharp),
              ),

              // Image.network(
              //   url,
              //   height: height,
              //   width: width,
              //   fit: fit,
              //   errorBuilder: (context, error, stackTrace) {
              //     return Container(
              //       color: Colors.green,
              //     );
              //   },
              //   // fit: BoxFit.cover,
              // ),
            ),
          ));

    default:
      return SizedBox(
        height: height,
        width: width,
        child: ClipOval(
          child: Image.network(
            url,
            height: height,
            width: width,
            fit: BoxFit.cover,
          ),
        ),
      );
  }
}
